package cha.app.simplegraphics;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.View;

public class SimpleSquareView extends View implements SensorEventListener{
  private static final String TAG = "SimpleSquareView";
  private static final int SQUARE_SIZE = 30;
  private static final int ZERO_AREA = 5;
  
  private ShapeDrawable square;
  private SensorManager sm;
  private int x_pos, y_pos;
  private int scr_height, scr_width;
  private Sensor s;
  
  public SimpleSquareView( Context context, int height, int width ) {
    super( context );
    square = new ShapeDrawable(new RectShape());  //draw the square
    x_pos = width/2; y_pos = height/2;  //calculate start position for the square
    scr_height = height; scr_width = width;
    
    //create a SensorManager object and grab a list of available orientation sensors
    sm = (SensorManager)context.getSystemService( Context.SENSOR_SERVICE );
    List <Sensor>orientSensor = sm.getSensorList( Sensor.TYPE_ORIENTATION );
    
    square.getPaint().setColor( Color.GREEN );  //make the square green!
    square.setBounds( x_pos, y_pos, x_pos + SQUARE_SIZE, y_pos + SQUARE_SIZE);  //place the square in the middle of the screen
    s = null;
    
    //check if there's at least 1 orientation sensor on the phone. If there is
    //use the first one.
    if(orientSensor.size() > 0){
      Log.d( TAG, "Orientation Sensor Found." );
      s = orientSensor.get(0);
      sm.registerListener( this, s, SensorManager.SENSOR_DELAY_UI );
    }
    else
      Log.d( TAG, "Orientation Sensor Missing." );
  }
  
  //stop all listeners, threads, etc...
  public void destroy(){
    if(!s.equals( null ))
      sm.unregisterListener( this, s );
  }

  protected void onDraw(Canvas canvas) {
    square.draw(canvas);
  }

  @Override
  public void onAccuracyChanged( Sensor sensor, int accuracy ) {
    ; //do nothing
  }

  @Override
  public void onSensorChanged( SensorEvent event ) {
    if(event.sensor.getType() == Sensor.TYPE_ORIENTATION){
      //orientation values (degrees)
      float yaw = event.values[0];
      float pitch = event.values[1];
      float roll = event.values[2];
      Log.d( TAG, yaw + " | " + pitch + " | " + roll );
      
      //change the squares coordinates based on sensor data
      if(Math.abs(roll) > ZERO_AREA)
        x_pos -= ((roll - ZERO_AREA)/3);
      if(Math.abs(pitch) > ZERO_AREA)
        y_pos -= ((pitch - ZERO_AREA)/3);
      
      //check if square is beyond minimum bounds
      x_pos = (x_pos < 0) ? 0 : x_pos;
      y_pos = (y_pos < 0) ? 0 : y_pos;
      
    //check if square is beyond maximum bounds
      x_pos = (x_pos + SQUARE_SIZE > scr_width) ? scr_width - SQUARE_SIZE : x_pos;
      y_pos = (y_pos + SQUARE_SIZE > scr_height) ? scr_height - SQUARE_SIZE : y_pos;
      
    }
    Log.d( TAG, x_pos + " | " + y_pos );
    square.setBounds( x_pos, y_pos, x_pos + SQUARE_SIZE, y_pos + SQUARE_SIZE);
    invalidate(); //redraw
  }
  
}
